//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   BODY
// AUTHOR           :   Ric Yik 
// DATE CREATE      :   2006/06/12
// DESCRIPTION      :   a DAO file for the function, BODY
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/06/12   Ric Yik         1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.common.itemTask.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.itemTask.pk.ItemTaskApprovalPk;
import com.erp.app.common.itemTask.vo.ItemTaskApprovalVo;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.dao.SimpleSearchMaintStyleDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.types.CustomBigInteger;
import com.erp.framework.types.CustomSqlDate;

/**
* <p>A <strong>DAO</strong>(Data Access Object) abstracts and encapsulates
* all access to the persistent store.  The DAO manages the connection with the
* data source to obtain and store data.  Also, the DAO implements
* the access mechanism required to work with the data source,
* and always provides a uniform API to its clients.
*
* @author          Ric Yik 
* @version 1.0     2006/06/12
* @since           FrameWork 1.0
*
*/
public abstract class ItemTaskApprovalDao extends SimpleSearchMaintStyleDao {

	/**
	* Returns an array list of child VOs by the parent
	* @param         parent   the incoming parent VO
	* @param         conn     the database connection
	* @return        an array list of child VOs
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	public ChildList selectByParent(SimpleBaseVo parent, Connection conn)
		throws FindRecordException {

		return super.selectByParent(parent, this.getItemTaskApprovalClass(), conn);
	}

	/**
	* Constructs a string of SQL insert statement
	* @return        a string of SQL insert statement
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected String sqlInsert() {

		String sql = "INSERT INTO T_ITEM_TASK_APPROVAL ("
			+ "COMPANY_CODE, DOCUMENT_NO, DOCUMENT_TYPE, TASK_LINE_NO, APPROVAL_LINE_NO, STATUS, "
			+ "COMPLETE_DATE, REMARKS, DC_IND, DC_DATE, CREATE_USER, "
			+ "CREATE_DATE, UPDATE_USER, UPDATE_DATE) "
			+ "VALUES ("
			+ "?, ?, ?, ?, ?, ?, "
			+ "?, ?, 'NO', ?, ?, "
			+ "SYSDATE, ?, ?)";
		return sql;
	}

	/**
	* Sets the values for a SQL insert or update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected void setValuesForInsertAndModify(PreparedStatement pStmt, Object obj, String opType)
		throws SQLException {

		ItemTaskApprovalVo vo = (ItemTaskApprovalVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setString(i++, vo.getDocumentNo());
		pStmt.setString(i++, vo.getDocumentType());
        pStmt.setInt(i++, vo.getApprovalTaskLineNo().intValue());
		pStmt.setInt(i++, vo.getApprovalLineNo().intValue());
		setNullableString(pStmt, i++, vo.getApprovalStatus());
		setNullableDate(pStmt, i++, vo.getApprovalCompleteDate());
		setNullableString(pStmt, i++, vo.getApprovalRemarks());

		if (ProjectConstants.DB_OP_UPDATE.equals(opType))
			setNullableString(pStmt, i++, vo.getApprovalDcInd());
		setNullableDate(pStmt, i++, vo.getApprovalDcDate());
		setNullableString(pStmt, i++, vo.getCreateUser());

		if (ProjectConstants.DB_OP_UPDATE.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getCreateDate());

		setNullableString(pStmt, i++, vo.getUpdateUser());

		if (ProjectConstants.DB_OP_INSERT.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getUpdateDate());

		if(ProjectConstants.DB_OP_UPDATE.equals(opType)){
			pStmt.setString(i++, vo.getCompanyCode());
			pStmt.setString(i++, vo.getDocumentNo());
			pStmt.setString(i++, vo.getDocumentType());
            pStmt.setInt(i++, vo.getApprovalTaskLineNo().intValue());
			pStmt.setInt(i++, vo.getApprovalLineNo().intValue());
			setNullableTimestamp(pStmt, i++, vo.getUpdateDate());
		}
	}

	/**
	* Sets the values for a SQL insert statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected void setValuesForInsert(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_INSERT);
	}

	/**
	* Constructs a string of SQL update statement
	* @return        a string of SQL update statement
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected String sqlUpdate() {

		String sql = "UPDATE T_ITEM_TASK_APPROVAL SET COMPANY_CODE=?, DOCUMENT_NO=?, DOCUMENT_TYPE=?, TASK_LINE_NO=?, APPROVAL_LINE_NO=?, "
			+ "STATUS=?, "
			+ "COMPLETE_DATE=?, REMARKS=?, DC_IND=?, DC_DATE=?, CREATE_USER=?, "
			+ "CREATE_DATE=?, UPDATE_USER=?, UPDATE_DATE=SYSDATE "
			+ "WHERE COMPANY_CODE=? AND DOCUMENT_NO=? AND DOCUMENT_TYPE=? AND TASK_LINE_NO=? AND APPROVAL_LINE_NO=? "
			+ "AND (UPDATE_DATE IS NULL OR UPDATE_DATE = ?) ";
		return sql;
	}

	/**
	* Sets the values for a SQL update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected void setValuesForModify(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_UPDATE);
	}

	/**
	* Constructs a string of SQL delete statement
	* @return        a string of SQL update statement
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected String sqlDelete() {
		String sql = "DELETE FROM T_ITEM_TASK_APPROVAL WHERE COMPANY_CODE=? AND DOCUMENT_NO=? AND DOCUMENT_TYPE=? AND TASK_LINE_NO=? AND APPROVAL_LINE_NO=? ";

		return sql;
	}

	/**
	* Sets the values for a SQL delete statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected void setValuesForDelete(PreparedStatement pStmt, Object obj)
		throws SQLException {

		ItemTaskApprovalVo vo = (ItemTaskApprovalVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setString(i++, vo.getDocumentNo());
		pStmt.setString(i++, vo.getDocumentType());
        pStmt.setInt(i++, vo.getApprovalTaskLineNo().intValue());
		pStmt.setInt(i++, vo.getApprovalLineNo().intValue());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected String sqlFind() {

		String sql = "SELECT COMPANY_CODE, DOCUMENT_NO, DOCUMENT_TYPE, APPROVAL_LINE_NO, STATUS, "
			+ "COMPLETE_DATE, REMARKS, DC_IND, DC_DATE, CREATE_USER, "
			+ "CREATE_DATE, UPDATE_USER, UPDATE_DATE "
			+ "FROM T_ITEM_TASK_APPROVAL "
			+ "WHERE COMPANY_CODE=? AND DOCUMENT_NO=? AND DOCUMENT_TYPE=? AND TASK_LINE_NO=? AND APPROVAL_LINE_NO=? "
			+ "ORDER BY COMPANY_CODE, DOCUMENT_NO, DOCUMENT_TYPE, TASK_LINE_NO, APPROVAL_LINE_NO";
		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding by primary key.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming PK
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected void setValuesForFind(PreparedStatement pStmt, Object obj)
		throws SQLException {

		ItemTaskApprovalPk pk = (ItemTaskApprovalPk) obj;
		int i = 1;

		pStmt.setString(i++, pk.getCompanyCode());
		pStmt.setString(i++, pk.getDocumentNo());
		pStmt.setString(i++, pk.getDocumentType());
        pStmt.setInt(i++, pk.getApprovalTaskLineNo().intValue());
		pStmt.setInt(i++, pk.getApprovalLineNo().intValue());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected String sqlList() {

		String sql = "SELECT COMPANY_CODE, DOCUMENT_NO, DOCUMENT_TYPE, APPROVAL_LINE_NO, STATUS, "
			+ "COMPLETE_DATE, REMARKS, DC_IND, DC_DATE, CREATE_USER, "
			+ "CREATE_DATE, UPDATE_USER, UPDATE_DATE "
			+ "FROM T_ITEM_TASK_APPROVAL "
			+ "WHERE COMPANY_CODE=? AND DOCUMENT_NO=? AND DOCUMENT_TYPE=? "
			+ "ORDER BY COMPANY_CODE, DOCUMENT_NO, DOCUMENT_TYPE, TASK_LINE_NO, APPROVAL_LINE_NO";
		return sql;
	}

	/**
	* Sets the values of a result set to a vo
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Ric Yik 
	* @since      Framework 1.0
	*/
	protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {

		ItemTaskApprovalVo vo = this.makeItemTaskApprovalVo();

		vo.setCompanyCode(rs.getString("COMPANY_CODE"));
		vo.setDocumentNo(rs.getString("DOCUMENT_NO"));
		vo.setDocumentType(rs.getString("DOCUMENT_TYPE"));
        vo.setApprovalTaskLineNo(CustomBigInteger.customize(rs.getBigDecimal("TASK_LINE_NO")));
		vo.setApprovalLineNo(CustomBigInteger.customize(rs.getBigDecimal("APPROVAL_LINE_NO")));
		vo.setApprovalStatus(rs.getString("STATUS"));
		vo.setApprovalCompleteDate(CustomSqlDate.customize(rs.getDate("COMPLETE_DATE")));
		vo.setApprovalRemarks(rs.getString("REMARKS"));
		vo.setApprovalDcInd(rs.getString("DC_IND"));
		vo.setApprovalDcDate(CustomSqlDate.customize(rs.getDate("DC_DATE")));
		vo.setCreateUser(rs.getString("CREATE_USER"));
		vo.setCreateDate(rs.getTimestamp("CREATE_DATE"));
		vo.setUpdateUser(rs.getString("UPDATE_USER"));
		vo.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));

		return vo;
	}

    protected abstract Class getItemTaskApprovalClass();

    protected abstract ItemTaskApprovalVo makeItemTaskApprovalVo();

}
